package com.sky.directdeal.fragment;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.sky.directdeal.R;
import com.sky.directdeal.utils.Utils;
import com.sky.directdeal.widget.MapWrapperLayout;
import com.sky.local_ads.store_list.StoreDetails;

public class BusinessMapFragment extends Fragment {
	private MapWrapperLayout mapWrapperLayout;
	private SupportMapFragment mapFragment;
	private GoogleMap googleMap;
	
	private List<StoreDetails> businessDetailsList;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = null;
		if (container != null) {
			if (container.getChildCount() == 0) {
				view = inflater.inflate(R.layout.fragment_business_map,
						container, false);
				mapWrapperLayout = (MapWrapperLayout) view
						.findViewById(R.id.mapRelativeLayout);
				return view;
			} else {
				mapWrapperLayout = (MapWrapperLayout) container.getChildAt(0)
						.findViewById(R.id.mapRelativeLayout);
				return container.getChildAt(0);
			}

		}
		return view;
	}
	
	public void setStoreList(List<StoreDetails> storeList) {
		this.businessDetailsList = storeList;
		addMarker();
	}
	
	//adding marker to map view
		public void addMarker() {
			if(googleMap!=null) {
				googleMap.clear();


				//storeListMarkerIdMap=new HashMap<String, StoreEventInfo>();
				int storeListSize = businessDetailsList.size();
				if (storeListSize == 0) {
					return;
				}
				
				LatLngBounds.Builder builder = new LatLngBounds.Builder();
				for (int i = 0; i < storeListSize; i++) {

					//StoreEventInfo eventInfo = new StoreEventInfo(i);
					StoreDetails storeDetails = businessDetailsList.get(i);
					LatLng position = new LatLng(storeDetails.getLatitude(),
							storeDetails.getLongitude());

					MarkerOptions markerOptions = new MarkerOptions();
					markerOptions.position(position);
					markerOptions.title(storeDetails.getBusinessName());
					markerOptions.snippet(storeDetails.getAddress());
					builder.include(markerOptions.getPosition());
					googleMap.addMarker(markerOptions);
					/*markerOptions.icon(BitmapDescriptorFactory
							.fromResource(R.drawable.store_list_pin));
					storeListMarkerIdMap.put(googleMap.addMarker(markerOptions).getId(),
							eventInfo);*/
					
					
					
				}
				
				LatLngBounds bounds = builder.build();

			//	CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
			//	googleMap.moveCamera(cu);
			}
		}

	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		int playServiceStatus=Utils.getPlayServiceStatus(getActivity());
	    if(playServiceStatus == ConnectionResult.SUCCESS) {
			FragmentManager fm = getFragmentManager();
			mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
			if (mapFragment == null) {
				mapFragment = SupportMapFragment.newInstance();;
			}
			googleMap = mapFragment.getMap();
			googleMap.getUiSettings().setZoomControlsEnabled(false);
			LatLng position=new LatLng(13.086654, 80.154767);
			CameraUpdate updatePosition = CameraUpdateFactory
					.newLatLng(position);
			CameraUpdate updateZoom = CameraUpdateFactory.zoomTo(10);
			googleMap.moveCamera(updatePosition);
			googleMap.animateCamera(updateZoom);
	}

	}
}
